﻿	

#region Using Directives
using System;
using System.ComponentModel;
using System.Collections;
using System.Xml.Serialization;
using System.Data;
using NetROS.Tiers.Data.Bases;
using NetROS.Tiers.Entities;
using NetROS.Tiers.Entities.Validation;

using NetROS.Tiers.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#endregion

namespace NetROS.Tiers.Services
{		
	
	///<summary>
	/// An component type implementation of the 'tblKlijent' table.
	///</summary>
	/// <remarks>
	/// All custom implementations should be done here.
	/// </remarks>
	[CLSCompliant(true)]
	public partial class KlijentService : NetROS.Tiers.Services.KlijentServiceBase
	{
		/// <summary>
		/// Initializes a new instance of the KlijentService class.
		/// </summary>
		public KlijentService() : base()
		{
		}
        ///<summary>
        ///</summary>
        ///<param name="entity"></param>
        ///<param name="_listaracuna"></param>
        ///<param name="deepSaveType"></param>
        ///<param name="childTypes"></param>
        ///<returns></returns>
        ///<exception cref="EntityNotValidException"></exception>
        [DataObjectMethod(DataObjectMethodType.Update)]
        public bool DeepSaveKlijentSRacunima(Klijent entity,TList<KlijentBankovniRacun> _listaracuna, DeepSaveType deepSaveType, params System.Type[] childTypes)
        {
            // throws security exception if not authorized
            SecurityContext.IsAuthorized("DeepSave");

            if (!entity.IsValid)
            {
                throw new Entities.EntityNotValidException(entity, "DeepSaveKlijentSRacunima");
            }

            bool result = false;
            TransactionManager transactionManager = null;

            try
            {
                bool isBorrowedTransaction = ConnectionScope.Current.HasTransaction;
                TList<KlijentBankovniRacun> _listaZaUpdFalse = new TList<KlijentBankovniRacun>();
                TList<KlijentBankovniRacun> _listaZaUpdTrue = new TList<KlijentBankovniRacun>();
                TList<KlijentBankovniRacun> _listanew = new TList<KlijentBankovniRacun>();
               


                //since this is a read operation, don't create a tran by default, only use tran if provided to us for custom isolation level
                transactionManager = ConnectionScope.ValidateOrCreateTransaction();
                NetTiersProvider dataProvider = ConnectionScope.Current.DataProvider;

                //Save
                result = dataProvider.KlijentProvider.DeepSave(transactionManager, entity, deepSaveType, childTypes);
                foreach (KlijentBankovniRacun _racun in _listaracuna)
                {

                    if (_racun.IsNew)
                    {
                        _racun.IDKlijent = entity.ID;
                        _listanew.Add(_racun);
                    }
                    else
                    {
                        if (_racun.IsDefault)
                        {
                            _listaZaUpdTrue.Add(_racun);
                        }
                        else
                        {
                            _listaZaUpdFalse.Add(_racun);
                        }
                    }


                }
                foreach (KlijentBankovniRacun ent in _listaracuna.DeletedItems)
                {
                    dataProvider.KlijentBankovniRacunProvider.Delete(transactionManager, ent);
                }
                dataProvider.KlijentBankovniRacunProvider.Save(transactionManager, _listaZaUpdFalse);
                dataProvider.KlijentBankovniRacunProvider.Save(transactionManager, _listaZaUpdTrue);
                dataProvider.KlijentBankovniRacunProvider.Save(transactionManager, _listanew);
                

                //If success, Commit
                if (!isBorrowedTransaction && transactionManager != null && transactionManager.IsOpen)
                    transactionManager.Commit();

            }
            catch (Exception exc)
            {
                //if open, rollback
                if (transactionManager != null && transactionManager.IsOpen)
                    transactionManager.Rollback();

                //Handle exception based on policy
                if (DomainUtil.HandleException(exc, "NoneExceptionPolicy"))
                    throw;

            }
            return result;
        }
	}//End Class


} // end namespace
